<?php
class Modulemanager_Model_Pagedecision implements Modulemanager_Model_Mainable{
    
    private $action_handler;
    private $core_controller_csrftoken;
    private $get_status_list;
    private $get_access_levels_list;
    private $modulemanager_data_selectimage;
    
    public function main(array $variables = null) {
        switch ($variables['get']['page']){
            case "dodaj-modul":
                return $this->add_new_module($variables);
                break;
            case "wstaw-modul":
                return $this->insert_new_module($variables);
                break;
            case "edytuj-modul":
                return $this->edit_module($variables);
                break;
            case "aktualizuj-modul":
                return $this->update_module($variables);
                break;
            case "usun-modul":
                return $this->delete_module($variables);
                break;
            default:
                return $this->default_action($variables);
                break;
        }
    }
    
    private function delete_module(array $variables = null){
        $this->action_handler = new Modulemanager_Model_Deletemodule;
        return $this->action_handler->main($variables);
    }
    
    private function update_module(array $variables = null){
        $this->action_handler = new Modulemanager_Model_Updatemodule;
        return $this->action_handler->main($variables);
    }
    
    private function edit_module(array $variables = null){
        if(isset($variables['get']['name'])){
            $page = $this->get_other_values($variables);
            $this->core_controller_csrftoken = new Core_Controller_Csrftoken;
            $this->action_handler = new Modulemanager_Data_Getcurrentmodule;
            $page['content'] = $this->action_handler->main($variables);
            $page['template'] = WEB_ROOT_FOLDER."modulemanager/views/add.php";
            $page['csrf_token'] = $this->core_controller_csrftoken->main($variables);
            return $page;
        }
        else {
            return $this->default_action($variables);
        }
    }
    
    private function insert_new_module(array $variables = null){
        $this->action_handler = new Modulemanager_Model_Insertmodule;
        return $this->action_handler->main($variables);
    }
    
    private function add_new_module(array $variables = null){
        $page = $this->get_other_values($variables);
        $this->core_controller_csrftoken = new Core_Controller_Csrftoken;
        $page['template'] = WEB_ROOT_FOLDER."modulemanager/views/add.php";
        $page['csrf_token'] = $this->core_controller_csrftoken->main($variables);
        return $page;
    }
    
    private function default_action(array $variables = null){
        unset($variables);
        $this->action_handler = new Modulemanager_Model_Modulemanager;
        return $this->action_handler->main($variables);
    }
    
    private function get_other_values(array $variables = null){
        $this->modulemanager_data_selectimage = new Modulemanager_Data_Selectimage;
        $page['page_images'] = $this->modulemanager_data_selectimage->main($variables);
        $this->get_access_levels_list = new Accountsettings_Data_Getaccesslevels;
        $page['access_levels'] = $this->get_access_levels_list->main($variables);
        $this->get_status_list = new Accountsettings_Data_Getstatus;
        $page['status_name'] = $this->get_status_list->main($variables);
        return $page;
    }

}
